package org.net
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.system.Security;
	import flash.utils.ByteArray;
	
	import org.event.ImgEvent;

	/**
	 * 图片上传
	 * @author anjue
	 */
	public class ImgUpload
	{
		private static var dispatch:EventDispatcher = new EventDispatcher();
		
		/** 图片上传域 **/
		public static var img_upload_url:String = "http://10.1.1.57:8080/";
		
		/** 图片下载域 **/
		public static var img_download_url:String = "http://10.1.1.57:7070/download.fcgi?";
		/**
		 * 上传图片
		 * @param _address  图片服务器地址
		 * @param by		图片二进制数据
		 */
		public static function upload(by:ByteArray):void
		{
			Security.loadPolicyFile(img_upload_url + "crossdomain.xml")
			var postData:ByteArray = FormPost.makePostData("file",by,{uid:"100700",channelid:"2"});
			var request:URLRequest = new URLRequest(img_upload_url + "cgi_upload.cgi");
			request.data = postData;
			request.method = URLRequestMethod.POST;
			request.contentType="multipart/form-data";
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE,complete);
			loader.addEventListener(IOErrorEvent.IO_ERROR,ioError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securrityError);
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.load(request);
		}
		
		private static function complete(e:Event):void
		{
			var by:ByteArray = e.target.data;
			by.position = 0;
			var relativePath:String = by.readUTFBytes(by.length);
			
			var eve:ImgEvent = new ImgEvent(Event.COMPLETE);
			eve.img_url = img_download_url+relativePath;
			dispatchEvent(eve);
		}
		
		private static function ioError(e:IOErrorEvent):void
		{
			var loader:URLLoader = e.target as URLLoader;
			loader.removeEventListener(Event.COMPLETE,complete);
			loader.removeEventListener(IOErrorEvent.IO_ERROR,ioError);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securrityError);
			trace(e);
		}
		
		private static function securrityError(e:SecurityErrorEvent):void
		{
			var loader:URLLoader = e.target as URLLoader;
			loader.removeEventListener(Event.COMPLETE,complete);
			loader.removeEventListener(IOErrorEvent.IO_ERROR,ioError);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securrityError);
			trace(e);
		}
		
		public static function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			dispatch.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		
		public static function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			dispatch.removeEventListener(type,listener,useCapture);
		}
		
		public static function dispatchEvent(event:Event):void
		{
			dispatch.dispatchEvent(event);
		}
	}
}